/*
 * @Author: czy
 * @Date: 2021-07-13 11:01:13
 * @LastEditTime: 2021-07-15 20:37:22
 */
#include "sys/type.h"
#include "sys/container.h"
#include "sys/system.h"

#include "../lib/state.h"
#include "../lib/scheduler.h"

// Sort by pid
extern Zset new_queue;
extern Zset waiting_queue;
// Sort by priority
extern Zset ready_queue;

PCB *current_process;

PCB *get_current_process(void)
{
    return current_process;
}

void schedule(void)
{
    if (ready_queue.length > 0)
    {
        idle_task_flag = 1;
        ready2running();
    }
    else
    {
        PCB *idle_task = (PCB *)(new_queue.find(&new_queue, 1));
        idle_task->PC = (unsigned int)idle_task_body;
        run_process(idle_task);
    }
}

int createProcess(int (*tskBody)(void))
{
    return null2new(tskBody);
}

void destroyProcess(int tskIndex)
{
    ready2termination(tskIndex);
}

void processStart(int tskIndex)
{
    new2ready(tskIndex);
}

void processSwitch(void)
{
    running2ready();
    schedule();
}

void processEnd(void)
{
    running2termination();
    schedule();
}
